package com.text4;

import java.sql.*;
import java.util.Date;

public class Add {
    public static void main(String[] args) throws SQLException {
        Add add = new Add();
        int count= add.insertNews(6,"12","3","4",5);
        if(count>0){
            System.out.println("新增成功");
        }

    }
    public int insertNews(int id,String userName ,String password, String email,int userType) throws SQLException{

        Connection connection=null;
        PreparedStatement preparedStatement=null;
        int row=0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/kgcnews";
            String username="root";
            String password1="123456";
            connection=DriverManager.getConnection(url,username,password1);

            String sql="INSERT INTO news_user(id,userName,password,email,userType)VALUES(?,?,?,?,?)";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.setString(2,userName);
            preparedStatement.setString(3,password);
            preparedStatement.setString(4,email);
            preparedStatement.setInt(5,userType);


            row= preparedStatement.executeUpdate();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            preparedStatement.close();
            connection.close();
        }

        return row;
    }

}
